package org.ccay.async;

import javax.inject.Inject;

import org.ccay.core.exception.ApplicationException;
import org.ccay.core.log.ILogger;
import org.ccay.core.log.CcayLoggerFactory;

/**
 * JMS消息监听器。
 * 负责监听特定的消息队列，从中获取消息，调用相应的消息处理器实现对消息的处理。
 * 
 * 
 * @since Dec 6, 2011
 */
public class JmsMessageListener implements IMessageProcessor {
	
	/** 日志记录器 **/
	private ILogger log = CcayLoggerFactory.getLogger(JmsMessageListener.class);
	
	@Inject
	private IAsyncFramework asyncFramework;

	/**
	 * 处理用户消息
	 * 	
	 * 
	 * @since Dec 6, 2011
	 * @param message
	 */
	public void process(AsyncMessage message) {
		log.debug("Received async message: "+message.getType());
		IMessageProcessor processor = asyncFramework.getMessageProcessor(message);
		if(processor != null) {
			try {
				processor.process(message);
			} catch (ApplicationException ex) {
				log.error(ex);
			} catch(RuntimeException e){
				log.error(e);
			} catch(Error e){
				//记录日志以便定位问题
				log.fatal(e);
				throw e;
			}
		}
	}

}